package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdminSensitiveMapper;
import com.heima.admin.service.AdminSensitiveService;
import com.heima.apis.wemedia.SensitiveClient;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Psycho
 * @create 2022-05-02-11:34
 */
@Service
@Slf4j
public class AdminSensitiveServiceImpl extends ServiceImpl<AdminSensitiveMapper, WmSensitive> implements AdminSensitiveService {

    @Autowired
    private SensitiveClient sensitiveClient;

    /**
     * 敏感词的查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listSensitives(SensitiveDto dto) {
        //参数的校验
        if(dto.getPage()==null || dto.getSize()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        IPage page = sensitiveClient.listSensitive(dto);
        long total = page.getTotal();
//        List<WmSensitive> wmSensitives1 = wmSensitives.subList((dto.getPage() - 1) * dto.getSize(), dto.getPage() * dto.getSize());
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int)total);
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /**
     * 删除某个敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteOneSensitive(Integer id) {
        return sensitiveClient.deleteOneSensitive(id);
    }

    /**
     * 修改某个敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateOneSensitive(AdSensitive adSensitive) {
        sensitiveClient.updateOneSensitive(adSensitive);
        return ResponseResult.okResult(0);
    }

    /**
     * 新增敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveOneSensitive(AdSensitive adSensitive) {
        sensitiveClient.saveOneSensitive(adSensitive);
        return ResponseResult.okResult(0);
    }
}
